package com.tepth.maintenancesystem.view;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tepth.maintenancesystem.R;
import com.tepth.maintenancesystem.SysApplication;
import com.tepth.maintenancesystem.api.MaintainHttp;
import com.tepth.maintenancesystem.api.OutLogin;
import com.tepth.maintenancesystem.bean.Contents;
import com.tepth.maintenancesystem.bean.MaintainDriverAndRemark;
import com.tepth.maintenancesystem.bean.MaintainListWork;
import com.tepth.maintenancesystem.bean.VehicleMaintain;
import com.tepth.maintenancesystem.adapter.MaintainDetailsCompletedRightAdapter;
import com.tepth.maintenancesystem.adapter.MaintainDetailsLeftAdapter;
import com.tepth.maintenancesystem.untils.ToastUtils;
import com.xiasuhuei321.loadingdialog.view.LoadingDialog;

import java.util.ArrayList;
import java.util.List;

public class MaintainHistoryDetailsActivity extends AppCompatActivity implements OutLogin {

    private MaintainDetailsLeftAdapter leftAdapter;
    private MaintainDetailsCompletedRightAdapter rightAdapter;
    private ListView leftListView;
    private ListView rightListView;
    private Context context;
    private ArrayList<MaintainListWork> listCompleteds = new ArrayList<>();
    private VehicleMaintain obj;
    private LoadingDialog loadingDialog;
    private RelativeLayout rl_history_back;
    private TextView tv_history_title, tv_maintain_remark;
    private MaintainDriverAndRemark remark;
    private LinearLayout linear_junjian_barcode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maintain_history_details);
        SysApplication.getInstance().addActivity(this);
        context = this;
        Bundle bundle = this.getIntent().getBundleExtra("MaintainBundle");
        obj = (VehicleMaintain) bundle.getSerializable("Maintain");

        tv_history_title = (TextView) findViewById(R.id.tv_work_history_title);
        String title = "";
        if (obj.getActualEndTime().equals(""))
            title = "正在保养";
        else
            title = obj.getActualEndTime() + "  完成保养";
        tv_history_title.setText(title);
        rl_history_back = (RelativeLayout) findViewById(R.id.rl_work_history_back);
        rl_history_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        initView();
        getDriverAndRemark();
    }

    private void initView() {
        leftListView = (ListView) findViewById(R.id.left_history_listview);
        rightListView = (ListView) findViewById(R.id.right_history_listview);
        tv_maintain_remark = (TextView) findViewById(R.id.tv_maintain_history_remark);
        linear_junjian_barcode=(LinearLayout)findViewById(R.id.linear_junjian_barcode);
        linear_junjian_barcode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putSerializable("Maintain", obj);
                intent.putExtra("MaintainBundle", bundle);
                intent.setClass(context, MaintainCompletedDoneActivity.class);
                startActivity(intent);
            }
        });
    }

    private void getDriverAndRemark() {
        if (remark != null) {
            getListWorkContentsNormal();
            return;
        }
        loadingDialog = new LoadingDialog(context);
        loadingDialog.setInterceptBack(false)
                .setLoadingText("正在加载...");
        loadingDialog.show();
        new Thread() {
            @Override
            public void run() {
                remark = MaintainHttp.getDriverAndRemark(context, obj.getId() + "");
                myHandler.sendEmptyMessage(0);
                super.run();
            }
        }.start();
    }

    /**
     * 请求保养项目列表
     */
    private void getListWorkContentsNormal() {
        if (listCompleteds.size() > 0) {
            initLeftListView();
            return;
        }
        new Thread() {
            @Override
            public void run() {
                listCompleteds = MaintainHttp.getListWork(context, obj.getId(), MaintainHistoryDetailsActivity.this);
                myHandler.sendEmptyMessage(1);
                super.run();
            }
        }.start();
    }

    private void initLeftListView() {
        leftAdapter = new MaintainDetailsLeftAdapter(this, listCompleteds);
        leftAdapter.setSelectPosition(0);
        leftListView.setAdapter(leftAdapter);
        initRightListView(listCompleteds.get(0).getListContents());
        leftListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                updataRightListView(position);
            }
        });
    }

    private void updataRightListView(int position) {
        leftAdapter.setSelectPosition(position);
        leftAdapter.notifyDataSetChanged();
        //更新第二ListView
        rightAdapter.setDatas(listCompleteds.get(position).getListContents());
        rightAdapter.notifyDataSetChanged();
    }

    private void initRightListView(List<Contents> listContents) {
        rightAdapter = new MaintainDetailsCompletedRightAdapter(this, listContents);
        rightListView.setAdapter(rightAdapter);
        rightListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Contents object = (Contents) rightAdapter.getItem(i);
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putParcelable("contents", object);
//                bundle.putString("workName",object.getWorkPersonName());
                bundle.putString("vehicleName", obj.getVehicleNumber());
                intent.putExtra("bundle", bundle);
                intent.setClass(context, MaintainWorkCompletedDetailsActivity.class);
                startActivity(intent);
            }
        });
    }

    private Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    tv_maintain_remark.setText("报修信息:" + remark.getRemark());
                    getListWorkContentsNormal();
                    break;
                case 1:
                    loadingDialog.close();
                    if (listCompleteds != null) {
                        initLeftListView();
                    } else {
                        ToastUtils.showToast(context, "网络错误");
                    }
                    break;
            }
            super.handleMessage(msg);
        }
    };

    @Override
    public void outLogin() {
        ToastUtils.showToast(context, "登录超时");
        Intent intent2 = new Intent();
        intent2.setClass(context, LoginActivity.class);
        intent2.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent2.putExtra("manual", "manual");
        startActivity(intent2);
    }
}
